package com.animee.day11.slice;
import com.animee.day11.bean.FilmInfoBean;
import com.animee.day11.utils.DisplayUtils;
import com.animee.day11.utils.ToastUtils;
import com.bumptech.glide.Glide;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.animee.day11.ResourceTable;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.window.dialog.ToastDialog;

public class FilmInfoAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    ListContainer lc;
    private FilmInfoBean film;
   Text titleTv,actorTv,typeTv,webTv,regionTv,timeTv,scoreTv,pnumTv;
   Image image;
   Rating rating;

   boolean isExpanded = false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_filminfo);
        film = intent.getSerializableParam("film");
        initView();
        setData();
    }

    private void setData() {
        titleTv.setText(film.getTitle());
        String actors = listToStr(film.getActors());
        actorTv.setText(actors);

        String type = listToStr(film.getTypes());
        typeTv.setText(type);

        webTv.setText(film.getUrl());

        String regions = listToStr(film.getRegions());
        regionTv.setText(regions);

        timeTv.setText(film.getRelease_date());
        scoreTv.setText(film.getScore());
        pnumTv.setText(film.getVote_count()+"人评价");

        String coverUrl = film.getCover_url();
        if (coverUrl==null||coverUrl.isEmpty()){
            image.setPixelMap(ResourceTable.Media_load_failed);
        }else{
            Glide.with(this).load(coverUrl)
                    .placeholder(ResourceTable.Media_load_failed)
                    .error(ResourceTable.Media_load_failed)
                    .into(image);
        }
        Float fscore = Float.valueOf(film.getScore())/2;
        rating.setScore(fscore);
        rating.setIsOperable(true);
        try {
            rating.setFilledElement(
                    new PixelMapElement(this.getResourceManager()
                            .getResource(ResourceTable.Media_ic_one_f)));
            rating.setUnfilledElement(
                    new PixelMapElement(this.getResourceManager()
                            .getResource(ResourceTable.Media_ic_one)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String listToStr(List<String>list){
        String msg = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size()-1){
                msg += (list.get(i));
            }else {
                msg += (list.get(i) + "/");
            }
        }
        return msg;
    }

    private void initView() {
        titleTv = findComponentById(ResourceTable.Id_film_title);
        actorTv = findComponentById(ResourceTable.Id_film_tv_actors);
        typeTv = findComponentById(ResourceTable.Id_film_tv_type);
        webTv = findComponentById(ResourceTable.Id_film_tv_web);
        regionTv = findComponentById(ResourceTable.Id_film_tv_country);
        timeTv = findComponentById(ResourceTable.Id_film_tv_time);
        scoreTv = findComponentById(ResourceTable.Id_film_tv_score);
        pnumTv = findComponentById(ResourceTable.Id_film_tv_num);
        image = findComponentById(ResourceTable.Id_film_iv);
        rating = findComponentById(ResourceTable.Id_film_rat);


        actorTv.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_film_tv_actors) {
            isExpanded = !isExpanded;
            if (isExpanded) {  //展开
                actorTv.setMaxTextLines(20);
            }else{   //收缩
                actorTv.setMaxTextLines(3);
            }
//            ToastUtils.showMsg(this,"展开么？"+isExpanded);
        }
    }
}
